package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemDao;
import com.itheima.domain.CheckItem;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 检查项业务实现类
 */
@Service(interfaceClass = CheckItemService.class)
public class CheckItemServiceImpl implements CheckItemService {
    @Autowired
    private CheckItemDao checkItemDao;

    /**
     * 添加检查项
     *
     * @param checkItem
     */
    @Override
    public void add(CheckItem checkItem) {
        if (ObjectUtils.isEmpty(checkItem) || StringUtils.isEmpty(checkItem.getCode()) || StringUtils.isEmpty(checkItem.getName())) {
            throw new RuntimeException("关键数据不能为空");
        }
        checkItemDao.add(checkItem);
    }

    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        if (currentPage == null || currentPage < 1) {
            currentPage = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = 10;
        }
        PageHelper.startPage(currentPage, pageSize);
        Page<CheckItem> checkItems = checkItemDao.selectByCondition(queryString);
        long total = checkItems.getTotal();
        List<CheckItem> result = checkItems.getResult();
        return new PageResult(total, result);
    }

    /**
     * 根据id删除检查项
     *
     * @param id
     */
    @Transactional
    @Override
    public void deleteById(Integer id) {
        long count = checkItemDao.findCountByCheckItemId(id);
        if (count > 0) {
            throw new RuntimeException("清除关联后执行删除操作");
        } else {
            checkItemDao.deleteById(id);
        }
    }

    /**
     * 根据id查找检查项
     *
     * @param id
     * @return
     */
    @Override
    public CheckItem findById(Integer id) {
        return checkItemDao.findById(id);
    }

    /**
     * 修改
     *
     * @param checkItem
     */
    @Transactional
    @Override
    public void update(CheckItem checkItem) {
        if (ObjectUtils.isEmpty(checkItem) || ObjectUtils.isEmpty(checkItem.getId())) {
            throw new RuntimeException("关键数据不能为空");
        }
        if (StringUtils.isEmpty(checkItem.getCode()) || StringUtils.isEmpty(checkItem.getName())) {
            throw new RuntimeException("关键数据不能为空");
        }
        checkItemDao.update(checkItem);
    }


    /**
     * 查询所有检查项用于检查组展示
     *
     * @return
     */
    @Override
    public List<CheckItem> findAll() {
        return checkItemDao.findAll();
    }
}
